package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 89311 on 2018/4/11.
 */

public class FXSQAdapter3 extends RecyclerView.Adapter<FXSQAdapter3.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private FX_SQ notice;
    private JSONObject job;

    public FXSQAdapter3(Context context, FX_SQ add) {
        this.context = context;
        this.notice = add;
    }

    @Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_sq_item_view03,parent,false));
    }


    @Override
    public void onBindViewHolder(final LinerViewHolder holder, final int position) {

        try {
            job = (JSONObject) jsonArray.get(position);
            Glide.with(context).load(job.get("images").toString()).into(holder.SQiv01);//
            holder.SQtv03.setText(Function.phoneHide4Start(job.get("account").toString()));//
            holder.SQtv06.setText(job.get("number").toString());//
            holder.SQtv02.setText(job.get("name").toString());//
            holder.SQtv04.setText(job.get("wechat").toString());//
            //System.out.println("type"+job.get("type").toString());
            String data= ConverToDate(job.get("claim_start_time").toString());
            holder.SQtv07.setText(data);
            if (job.get("type").toString().equals("1"))
            {
                holder.SQbtn03.setBackgroundResource(R.drawable.button_index_css_hui);
            }
                holder.SQbtn03.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            if (((JSONObject) jsonArray.get(position)).get("type").toString().equals("1")){
                                Toast.makeText(context, "您已赠送", Toast.LENGTH_SHORT).show();
                            }else {
                        Object user_info = Preferences.getParam(context, "user_info", "");
                        JSONObject myJson = null;
                        String user_id = null;
                        try {
                            myJson = new JSONObject(user_info.toString());
                            user_id = myJson.get("id").toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //int giveid, int myid, int credits, int id
                                inits(((JSONObject) jsonArray.get(position)).get("giveid").toString(), user_id, ((JSONObject) jsonArray.get(position)).get("number").toString(), ((JSONObject) jsonArray.get(position)).get("id").toString(), holder.SQbtn03);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //System.out.println("删除"+position);
                    try {
                        init(((JSONObject) jsonArray.get(position)) .get("id").toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        jsonArray = notice.getJsonArray3();
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
            {
                return 0;
            }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private CircleImageView SQiv01;//
        private TextView SQtv03;//
        private TextView SQtv04;
        private TextView SQtv02;
        private TextView SQtv06;
        private Button SQbtn03;//
        private TextView SQtv07;
        private Button btn_del;
        public LinerViewHolder(View itemView) {
            super(itemView);
            SQiv01 = itemView.findViewById(R.id.SQiv01);//
            SQtv03 = itemView.findViewById(R.id.SQtv03);//
            SQtv04 = itemView.findViewById(R.id.SQtv04);//
            SQtv06 = itemView.findViewById(R.id.SQtv06);//
            SQtv02 = itemView.findViewById(R.id.SQtv02);//
            SQtv07 = itemView.findViewById(R.id.SQtv07);//
            SQbtn03 = itemView.findViewById(R.id.SQbtn03);//
            btn_del = itemView.findViewById(R.id.btn_del);//
        }
    }




    /**
     * 生成星星
     * @param index
     */
    private void createStart(int index,FrameLayout SQNfl01_){
        for (int i = 0; i < 4; i++){
            ImageView img = new ImageView(context);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(context,22);
            params.height = Function.dip2px(context,22);
            params.leftMargin = 4+i*Function.dip2px(context,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.img_0022));
            SQNfl01_.addView(img);
        }

        for (int i = 0; i < index; i++){
            ImageView img = new ImageView(context);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(context,22);
            params.height = Function.dip2px(context,22);
            params.leftMargin = 4+i*Function.dip2px(context,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.img_0023));

            SQNfl01_.addView(img);
        }
    }

    /**
     *生成时间
     * @param strDate
     * @return
     * @throws Exception
     */
    public final String ConverToDate(String strDate) throws Exception
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d= df.parse(strDate);
        String time = df.format(d);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        if (time.equals(date))
        {
            DateFormat df1 = new SimpleDateFormat("hh:mm");
            Date d1= df.parse(strDate);
            String a= df1.format(d1);
            return " 今天:"+a+" ";
        }else
        {
            return " "+time+" ";
        }

    }
        //int giveid, int myid, int credits, int id
    public void inits(String giveid, String myid, String credits, String id, final Button btn)
    {
        OKManager.getInstance().type2011(giveid,myid,credits,id, new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        btn.setBackgroundResource(R.drawable.button_index_css_hui);
                        btn.setText("已赠送");
                        Toast.makeText(context, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });
    }

    public void init(String id)
    {
        OKManager.getInstance().type2017(id, new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        Toast.makeText(context, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        refresh();
                    } else {
                        Toast.makeText(context, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });
    }

    public void refresh() {
        notice.finish();
        Intent intent = new Intent(context, FX_SQ.class);
        intent.putExtra("type","2");
        context.startActivity(intent);
        notice.overridePendingTransition(0,0);
    }


}
